package com.servlet;

import com.dao.*;
import com.entity.user;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("name");
        String pwd=req.getParameter("pwd");
        String name="";
        searchdao s=new searchdaoimpl();
        String yemian=s.searchcookiee(req);
        logindao l=new logindaoimpl();
        UserInfDao u=new UserInfDao();
        String sql="select * from user where UserID=?";
        List<user> list =u.getForList(user.class,sql,id);
        String uname=list.get(0).getUserName();
        if(uname!=null)
        {
            name=name.concat(uname);
        }
        if(l.Login(id,pwd))
        {
            req.setAttribute("message",name);
            Cookie cookie=new Cookie("dlid",id);
            Cookie cookie1=new Cookie("dlmm",pwd);
            resp.addCookie(cookie);resp.addCookie(cookie1);
            if ("zhuye".equals(yemian)) {
                req.getRequestDispatcher("/zhuye.jsp").forward(req,resp);
            }
            else if ("zhdt".equals(yemian)) {
                req.getRequestDispatcher("/zuhaodating.jsp").forward(req,resp);
            }
            else if ("mh".equals(yemian)) {
                req.getRequestDispatcher("/maihao.jsp").forward(req,resp);
            }
            else if("grzy".equals(yemian))
            {
                req.getRequestDispatcher("/grzhuye.jsp").forward(req,resp);
            }
        }
        else if(!(l.Login(id,pwd)))
        {
            req.setAttribute("message","用户名或密码不正确");
            req.getRequestDispatcher("/denglu.jsp").forward(req,resp);
        }
    }
}
